package com.irdstudio.efp.esb.api.rest.yed.sx;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.rest.yed.req.YedCreditRiskApproveNoticeReqBean;
import com.irdstudio.efp.esb.api.service.yed.NlsYedCreditNoticeProcess;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 需求编号：优e贷<br/>
 * 问题编号：<br/>
 * 功能描述：<br/>
 * 开发人员：dengqiwen<br/>
 * 创建时间：2020/7/30 14:45
 */
@RestController
@RequestMapping("/esb")
public class YedCreditRiskApproveNoticeController implements EsbController {

    // 日志ID
    private static final String LOGGER_ID = "优e贷-授信风控结果通知";

    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(YedCreditRiskApproveNoticeController.class);

    @PostMapping("/SERVICE_30220009_13")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @Validated({GroupeInterface.yed.class}) @ModelAttribute("YedCreditRiskApproveNoticeReqBean") YedCreditRiskApproveNoticeReqBean yedCreditRiskApproveNoticeReqBean, BindingResult bindingResult2, Exception exception) {
        // 全局关联流水号
        String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
        try {
            // 参数校验
            validateBean(esbReqServiceBean, bindingResult1, yedCreditRiskApproveNoticeReqBean, bindingResult2, exception, logger);
            // 授信申请流水号

            logger.info("======>优e贷授信通知 调用接口【"+glblSrlNo+"_30220009_13】开始<======");

            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);

            // 执行授信请求流程并返回
             nlsYedCreditNoticeProcess.processCreditResult(yedCreditRiskApproveNoticeReqBean);
            // 执行授信请求流程并返回
            return EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean);

        } catch (ESBException e) {
            logger.error(LOGGER_ID,"======>调用接口【" + glblSrlNo + "-30220009_10】异常<======"+e.getMessage());
            e.printStackTrace();
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, e.getErrorCode(), e.getMessage());
        } catch (BizException e) {
            logger.error(LOGGER_ID,"======>调用接口【" + glblSrlNo + "30220009_10】异常<======"+e.getMessage());
            e.printStackTrace();
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, e.getErrorCode(), e.getMessage());
        }finally {
            logger.info("======>优e贷授信通知 调用结果【"+glblSrlNo+"_30220009_05】结束<======");
        }
    }

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    @ModelAttribute("YedCreditRiskApproveNoticeReqBean")
    public YedCreditRiskApproveNoticeReqBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, YedCreditRiskApproveNoticeReqBean.class);
    }

    // 授信结果通知
    @Autowired
    private NlsYedCreditNoticeProcess nlsYedCreditNoticeProcess;
}
